﻿using Microsoft.Win32;
using System.Windows.Forms;

namespace KstopaCommon
{
    public class RegistryHelper
    {
        /// <summary>
        /// 读取指定名称的注册表的值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetRegistryData(string subkey, string name)
        {
            string registData = "";
            RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\\" + subkey, true);
            if (myKey != null)
            {
                registData = myKey.GetValue(name).ToString();
            }
            return registData;
        }

        /// <summary>
        /// 向注册表中写数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="tovalue"></param> 
        public void SetRegistryData(string subkey, string name, string value)
        {
            RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\\" + subkey, true);
            myKey.SetValue(name, value);
        }

        /// <summary>
        /// 删除注册表中指定的注册表项
        /// </summary>
        /// <param name="name"></param>
        public void DeleteRegistSubkeyName(string subkey, string name)
        {
            string[] subkeyNames;
            RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\\" + subkey, true);
            subkeyNames = myKey.GetSubKeyNames();
            foreach (string aimKey in subkeyNames)
            {
                if (aimKey == name)
                    myKey.DeleteSubKeyTree(name);
            }
        }

        /// <summary>
        ///创建注册表中指定的注册表项
        /// </summary>
        /// <param name="name"></param>
        public void CreateRegistSubkeyName(string subkey, string name)
        {
            RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\\" + subkey, true);
            myKey.SetValue(name, 0);
        }

        /// <summary>
        /// 判断指定注册表项是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsRegistryExist(string subkey, string name)
        {
            bool _exit = false;
            string[] subkeyNames;
            RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\\" + subkey, true);
            subkeyNames = myKey.GetValueNames();
            foreach (string keyName in subkeyNames)
            {
                if (keyName == name)
                {
                    _exit = true;
                    return _exit;
                }
            }

            return _exit;
        }

        /// <summary>
        /// 创建新注册表
        /// </summary>
        /// <param name="root"></param>
        /// <param name="subkey"></param>
        public void CreateRegist(string subkey)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software");
            string[] keynames = key.GetSubKeyNames();
            key.Close();
            bool isexist = false;
            foreach (string item in keynames)
            {
                if (item == subkey)
                {
                    //Registry.CurrentUser.DeleteSubKeyTree(@"Software\\" + subkey);
                    //Thread.Sleep(10);
                    isexist = true;
                    break;
                }
            }
            if (isexist==false)
            {
                Registry.CurrentUser.CreateSubKey(@"Software\\" + subkey);
            }
        }

        public bool CheckRegist(string subkey,uint inputNum,uint outputnum)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software");
                string[] keynames = key.GetSubKeyNames();
                int count = 0;
                foreach (string item in keynames)
                {
                    if (item == subkey)
                    {
                        break;
                    }
                    count++;
                }
                if (count == keynames.Length)
                {
                    Registry.CurrentUser.CreateSubKey(@"Software\\" + subkey);
                }

                for (int i = 0; i < inputNum; i++)
                {
                    if (IsRegistryExist(subkey, "X" + i.ToString()) == false)
                    {
                        CreateRegistSubkeyName(subkey, "X" + i.ToString());
                    }
                }

                for (int i = 0; i < outputnum; i++)
                {
                    if (IsRegistryExist(subkey, "Y" + i.ToString()) == false)
                    {
                        CreateRegistSubkeyName(subkey, "Y" + i.ToString());
                    }
                }
                return true;
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(exc.Message);
                return false;
            }
            
        }

    }
}
